<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-copy"></i> 反馈消息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="message">
                <el-tab-pane :label="`未读消息(${unread.length})`" name="first">
                    <el-table :data="unread" :show-header="false" style="width: 100%">
                        <el-table-column type="expand">
                            <template slot-scope="props">
                                <el-form label-position="left" inline class="demo-table-expand">
                                    <el-form-item class="half-width" label="用户手机">
                                        <span>{{ props.row.feedbackPhone }}</span>
                                    </el-form-item>
                                    <el-form-item class="half-width" label="用户邮箱">
                                        <span>{{ props.row.feedbackEmail }}</span>
                                    </el-form-item>
                                </el-form>
                                <el-form label-position="left" inline class="demo-table-expand">
                                    <el-form-item class="thirty-width" label="省">
                                        <span>{{ props.row.feedbackProv }}</span>
                                    </el-form-item>
                                    <el-form-item class="thirty-width" label="市">
                                        <span>{{ props.row.feedbackCity }}</span>
                                    </el-form-item>
                                    <el-form-item class="thirty-width" label="区">
                                        <span>{{ props.row.feedbackDistrict }}</span>
                                    </el-form-item>
                                </el-form>
                                <el-form label-position="left" inline class="demo-table-expand">
                                    <el-form-item class="thirty-width" label="内容">
                                        <span>{{ props.row.feedbackContent }}</span>
                                    </el-form-item>

                                </el-form>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <span class="message-title">【反馈通知】{{scope.row.feedbackContent}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="feedbackTime" width="150"></el-table-column>
                        <el-table-column width="120">
                            <template slot-scope="scope">
                                <el-button type="primary" @click="handleRead(scope.$index, scope.row)">标为已读</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination">
                        <el-pagination
                                background
                                layout="sizes, total, prev, pager, next"
                                :page-sizes="[5, 10, 20, 50]"
                                :current-page="unReadQuery.pageIndex"
                                :page-size="unReadQuery.pageSize"
                                :total="unReadPageTotal"
                                @size-change="handleUnReadSizeChange"
                                @current-change="handleUnReadPageChange"
                        ></el-pagination>
                    </div>
                    <div class="handle-row">
                        <el-button type="primary" @click="clearUnRead">全部标为已读</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane :label="`已读消息(${read.length})`" name="second">
                    <template v-if="message === 'second'">
                        <el-table :data="read" :show-header="false" style="width: 100%">
                            <el-table-column type="expand">
                                <template slot-scope="props">
                                    <el-form label-position="left" inline class="demo-table-expand">
                                        <el-form-item class="half-width" label="用户手机">
                                            <span>{{ props.row.feedbackPhone }}</span>
                                        </el-form-item>
                                        <el-form-item class="half-width" label="用户邮箱">
                                            <span>{{ props.row.feedbackEmail }}</span>
                                        </el-form-item>
                                    </el-form>
                                    <el-form label-position="left" inline class="demo-table-expand">
                                        <el-form-item class="thirty-width" label="省">
                                            <span>{{ props.row.feedbackProv }}</span>
                                        </el-form-item>
                                        <el-form-item class="thirty-width" label="市">
                                            <span>{{ props.row.feedbackCity }}</span>
                                        </el-form-item>
                                        <el-form-item class="thirty-width" label="区">
                                            <span>{{ props.row.feedbackDistrict }}</span>
                                        </el-form-item>
                                    </el-form>
                                    <el-form label-position="left" inline class="demo-table-expand">
                                        <el-form-item class="thirty-width" label="内容">
                                            <span>{{ props.row.feedbackContent }}</span>
                                        </el-form-item>

                                    </el-form>
                                </template>
                            </el-table-column>
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">【反馈通知】{{scope.row.feedbackContent}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="feedbackTime" width="150"></el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button type="danger" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination">
                            <el-pagination
                                    background
                                    layout="sizes, total, prev, pager, next"
                                    :page-sizes="[5, 10, 20, 50]"
                                    :current-page="readQuery.pageIndex"
                                    :page-size="readQuery.pageSize"
                                    :total="readPageTotal"
                                    @size-change="handleReadSizeChange"
                                    @current-change="handleReadPageChange"
                            ></el-pagination>
                        </div>
                        <div class="handle-row">
                            <el-button type="danger" @click="clearRead">删除全部</el-button>
                        </div>
                    </template>
                </el-tab-pane>
                <el-tab-pane :label="`回收站(${recycle.length})`" name="third">
                    <template v-if="message === 'third'">
                        <el-table :data="recycle" :show-header="false" style="width: 100%">
                            <el-table-column type="expand">
                                <template slot-scope="props">
                                    <el-form label-position="left" inline class="demo-table-expand">
                                        <el-form-item class="half-width" label="用户手机">
                                            <span>{{ props.row.feedbackPhone }}</span>
                                        </el-form-item>
                                        <el-form-item class="half-width" label="用户邮箱">
                                            <span>{{ props.row.feedbackEmail }}</span>
                                        </el-form-item>
                                    </el-form>
                                    <el-form label-position="left" inline class="demo-table-expand">
                                        <el-form-item class="thirty-width" label="省">
                                            <span>{{ props.row.feedbackProv }}</span>
                                        </el-form-item>
                                        <el-form-item class="thirty-width" label="市">
                                            <span>{{ props.row.feedbackCity }}</span>
                                        </el-form-item>
                                        <el-form-item class="thirty-width" label="区">
                                            <span>{{ props.row.feedbackDistrict }}</span>
                                        </el-form-item>
                                    </el-form>
                                    <el-form label-position="left" inline class="demo-table-expand">
                                        <el-form-item class="thirty-width" label="内容">
                                            <span>{{ props.row.feedbackContent }}</span>
                                        </el-form-item>

                                    </el-form>
                                </template>
                            </el-table-column>
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">【反馈通知】{{scope.row.feedbackContent}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="feedbackTime" width="150"></el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button @click="handleRestore(scope.$index, scope.row)">还原</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination">
                            <el-pagination
                                    background
                                    layout="sizes, total, prev, pager, next"
                                    :page-sizes="[5, 10, 20, 50]"
                                    :current-page="deleteQuery.pageIndex"
                                    :page-size="deleteQuery.pageSize"
                                    :total="deletePageTotal"
                                    @size-change="handleDeleteSizeChange"
                                    @current-change="handleDeletePageChange"
                            ></el-pagination>
                        </div>
                        <div class="handle-row">
                            <el-button type="danger" @click="clearRecycle">清空回收站</el-button>
                        </div>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
    import bus from '../common/bus';
    export default {
        name: 'tabs',
        data() {
            return {
                unReadQuery:{
                  pageIndex: 1,
                  pageSize: 10
                },
                unReadPageTotal:0,
                readQuery:{
                    pageIndex: 1,
                    pageSize: 10
                },
                readPageTotal:0,
                deleteQuery:{
                    pageIndex: 1,
                    pageSize: 10
                },
                deletePageTotal:0,

                message: 'first',
                showHeader: false,
                unread: [],
                read: [],
                recycle: []
            }
        },
        created(){
          this.init();
        },
        methods: {
            init(){
                this.$axios.get(this.GLOBAL.httpUrl + "/feedback/selectUnRead",{
                    params: this.unReadQuery
                }).then(res=>{
                    for(let i=0;i<res.data.data.resultList.length;i++){
                        res.data.data.resultList[i].feedbackTime = this.dateFormat(res.data.data.resultList[i].feedbackTime);
                    }
                    this.unReadPageTotal = res.data.data.totalCount;
                    this.unread = res.data.data.resultList;
                    bus.$emit('message', res.data.data.totalCount);
                })
                this.$axios.get(this.GLOBAL.httpUrl + "/feedback/selectRead",{
                    params: this.readQuery
                }).then(res=>{
                    for(let i=0;i<res.data.data.resultList.length;i++){
                        res.data.data.resultList[i].feedbackTime = this.dateFormat(res.data.data.resultList[i].feedbackTime);
                    }
                    this.readPageTotal = res.data.data.totalCount;
                    this.read = res.data.data.resultList;
                })

                this.$axios.get(this.GLOBAL.httpUrl + "/feedback/selectDelete",{
                    params: this.deleteQuery
                }).then(res=>{
                    for(let i=0;i<res.data.data.resultList.length;i++){
                        res.data.data.resultList[i].feedbackTime = this.dateFormat(res.data.data.resultList[i].feedbackTime);
                    }
                    this.recycle = res.data.data.resultList;
                    this.deletePageTotal = res.data.data.totalCount;
                })
            },
            handleRead(index, row) {
                const item = this.unread.splice(index, 1);
                this.read = item.concat(this.read);
                this.$axios.get(this.GLOBAL.httpUrl + "/feedback/updateReadById",{
                    params:{
                        feedbackId: row.feedbackId
                    }
                }).then(res=>{
                    if(res.data.status != 200){
                        this.$message.fail("啊哦~~已读失败了哟")
                    }else{
                        bus.$emit('message', this.unread.length);
                    }
                }).catch(error=>{
                    this.$message.error("啊哦~~已读异常了哟")
                })
            },
            handleDel(index, row) {
                const item = this.read.splice(index, 1);
                this.recycle = item.concat(this.recycle);
                this.$axios.get(this.GLOBAL.httpUrl + "/feedback/deleteById",{
                    params:{
                        feedbackId: row.feedbackId
                    }
                }).then(res=>{
                    if(res.data.status != 200){
                        this.$message.fail("啊哦~~删除失败了哟")
                    }
                }).catch(error=>{
                    this.$message.error("啊哦~~删除异常了哟")
                })
            },
            handleRestore(index, row) {
                const item = this.recycle.splice(index, 1);
                this.read = item.concat(this.read);
                this.$axios.get(this.GLOBAL.httpUrl + "/feedback/updateReadById",{
                    params:{
                        feedbackId: row.feedbackId
                    }
                }).then(res=>{
                    if(res.data.status != 200){
                        this.$message.fail("啊哦~~还原失败了哟")
                    }
                }).catch(error=>{
                    this.$message.error("啊哦~~还原异常了哟")
                })
            },
            handleUnReadPageChange(val){
                this.$set(this.unReadQuery, 'pageIndex', val);
                this.init();
            },
            handleReadPageChange(val){
                this.$set(this.readQuery, 'pageIndex', val);
                this.init();
            },
            handleDeletePageChange(val){
                this.$set(this.deleteQuery, 'pageIndex', val);
                this.init();
            },
            handleUnReadSizeChange(val){
                this.$set(this.unReadQuery, 'pageSize', val);
                this.init();
            },
            handleReadSizeChange(val){
                this.$set(this.readQuery, 'pageSize', val);
                this.init();
            },
            handleDeleteSizeChange(val){
                this.$set(this.deleteQuery, 'pageSize', val);
                this.init();
            },
            clearUnRead(){
                let params = {
                    readList: JSON.stringify(this.unread)
                }
                this.$axios.post(this.GLOBAL.httpUrl + "/feedback/updateReadByIdList", params)
                    .then(res=>{
                        if(res.data.status == 200){
                            this.$message.success("恭喜恭喜！操作成功啦！！");
                            this.init();
                        }else{
                            this.$message.warning("很遗憾！操作失败！！")
                        }
                    })
                    .catch(error=>{
                        this.$message.error("啊哦~~操作异常了哟")
                    })
            },
            clearRead(){
                let params = {
                    deleteList: JSON.stringify(this.read)
                }
                this.$axios.post(this.GLOBAL.httpUrl + "/feedback/deleteByList", params)
                    .then(res=>{
                        if(res.data.status == 200){
                            this.$message.success("恭喜恭喜！操作成功啦！！");
                            this.init();
                        }else{
                            this.$message.warning("很遗憾！操作失败！！")
                        }
                    })
                    .catch(error=>{
                        this.$message.error("啊哦~~操作异常了哟")
                    })
            },
            clearRecycle(){
                this.$confirm('此操作将永久删除当前页反馈通知, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let params = {
                        deleteList: JSON.stringify(this.recycle)
                    }
                    this.$axios.post(this.GLOBAL.httpUrl + "/feedback/deleteByPrimaryKeyList", params)
                        .then(res=>{
                            if(res.data.status == 200){
                                this.$message.success("恭喜恭喜！操作成功啦！！");
                                this.init();
                            }else{
                                this.$message.warning("很遗憾！操作失败！！")
                            }
                        })
                        .catch(error=>{
                            this.$message.error("啊哦~~操作异常了哟")
                        })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消清空'
                    });
                });
            }

        },
        computed: {
            unreadNum(){
                return this.unread.length;
            }
        }
    }

</script>

<style>
.message-title{
    cursor: pointer;
}
.handle-row{
    margin-top: 30px;
}
.demo-table-expand {
    font-size: 0;
}
.demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}
.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
}
.half-width{
    width: 100%;
}
.thirty-width{
    width: 30%;
}
</style>

